﻿#region Copyright
// Copyright (c) 2011-2012 RaisingForce Team (Alikin Sergey)
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.Diagnostics.Contracts;
using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace RaisingForce.Raise.Presentation
{
    sealed public class ElementErrorFinder
    {
        #region Constructors

        public ElementErrorFinder(DependencyObject root)
        {
            if (root == null)
            {
                throw new ArgumentNullException("root");
            }
            Contract.EndContractBlock();
            this.root = root;
        }

        #endregion

        #region Private Declarations

        private readonly DependencyObject root;

        private static void getErrors<T>(
            Func<T, string> getElementText,
            StringBuilder sb,
            DependencyObject obj)
            where T : DependencyObject
        {
            foreach (object child in LogicalTreeHelper.GetChildren(obj))
            {
                T element = child as T;
                if (element == null)
                {
                    continue;
                }
                if (Validation.GetHasError(element))
                {
                    if (sb.Length > 0)
                    {
                        sb.AppendLine();
                    }
                    sb.Append('"');
                    sb.Append(getElementText(element));
                    sb.AppendLine("\" contains errors:");
                    foreach (ValidationError error in Validation.GetErrors(element))
                    {
                        sb.Append(' ');
                        sb.AppendLine(error.ErrorContent.ToString());
                    }
                }
                ElementErrorFinder.getErrors<T>(getElementText, sb, element);
            }
        }

        #endregion

        #region Public Methods

        public bool HasErrors<T>(
            Func<T, string> getElementText,
            out string errorsMessage)
            where T : DependencyObject
        {
            if (getElementText == null)
            {
                throw new ArgumentNullException("getElementText");
            }
            Contract.Ensures(Contract.ValueAtReturn<string>(out errorsMessage) != null);
            Contract.EndContractBlock();
            var sb = new StringBuilder();
            ElementErrorFinder.getErrors<T>(getElementText, sb, this.root);
            errorsMessage = sb.ToString();
            return errorsMessage.Length > 0;
        }

        #endregion
    }
}
